﻿Imports System.Data.SqlClient
Imports AjaxControlToolkit

Public Class AddBook
    Inherits System.Web.UI.Page
    Dim LoadDanhMuc As New DanhMuc_BLL()
    Dim ThemSach As New Sach_BLL()

    Dim p As New Sach_DTO()
    Dim d As New DanhMuc_DTO()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            load_danhmuc()
            load_vitrike()

        Catch ex As Exception

        End Try
        If Not IsPostBack Then
            Session("hinhsach") = String.Empty
        End If
    End Sub

    Public Sub load_danhmuc()
        cb_danhmuc.DataSource = LoadDanhMuc.DanhMuc()
        cb_danhmuc.DataBind()
    End Sub

    Public Sub load_phanloai(ByVal tendm As String)
        d._tendm = tendm
        rdbtn_phanloai.DataSource = LoadDanhMuc.Sub_DanhMuc(d)
        rdbtn_phanloai.TextField = "phanloai"
        rdbtn_phanloai.ValueField = "phanloai"
        rdbtn_phanloai.DataBind()
    End Sub

    Public Sub clearAll()
        tb_masach.Text = ""
        tb_tensach.Text = ""
        tb_chitiet.Text = ""
        tb_tacgia.Text = ""
        tb_nxb.Text = ""
        rdbtn_phanloai.Visible = False
        rdbtn_hang.Visible = False
        cb_danhmuc.Text = ""
        cb_ke.Text = ""
    End Sub

    Protected Sub cb_danhmuc_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cb_danhmuc.SelectedIndexChanged
        Try
            rdbtn_phanloai.Visible = True
            load_phanloai(cb_danhmuc.Text)
        Catch

        End Try
       
    End Sub

    Public Sub load_vitrike()
        cb_ke.DataSource = ThemSach.Vitri_Ke()
        cb_ke.DataBind()
    End Sub

    Protected Sub cb_ke_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cb_ke.SelectedIndexChanged
        rdbtn_hang.Visible = True

    End Sub
    Protected Sub bt_them_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_them.Click

        Try
            p._idsach = tb_masach.Text
            p._ten = tb_tensach.Text
            p._chitiet = tb_chitiet.Text
            p.tacgia = tb_tacgia.Text
            p._nxb = tb_nxb.Text
            p._soluong = sp_soluong.Text
            If Session("hinhsach") = String.Empty Then
                p._hinhanh = String.Empty
            Else
                p._hinhanh = "/img/book/" + Session("hinhsach")
                Session("hinhsach") = String.Empty
            End If
            p._danhmuc = cb_danhmuc.Text
            p._phanloai = rdbtn_phanloai.SelectedItem.Value.ToString()
            p._ke = cb_ke.SelectedItem.Text
            p._hang = rdbtn_hang.SelectedItem.Value.ToString()
            clearAll()
            ThemSach.ThemSach(p)
            successDiv.Attributes("class") = "alert alert-success"
            successDiv.Visible = True
            lb_mess.Text = "Thêm Thành Công!"

        Catch ex As SqlException
            successDiv.Attributes("class") = "alert alert-warning"
            successDiv.Visible = True
            lb_mess.Text = "Mã Sách Đã Tồn Tại!"
        End Try


    End Sub

    Protected Sub bt_hoantac_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_hoantac.Click
        clearAll()
    End Sub
   
    Protected Sub upload_sach_FileUploadComplete(ByVal sender As Object, ByVal e As DevExpress.Web.FileUploadCompleteEventArgs) Handles upload_sach.FileUploadComplete


        Dim filename As String = e.UploadedFile.FileName
        e.UploadedFile.SaveAs(MapPath("~/img/book/") + filename)
        Session("hinhsach") = filename
        e.CallbackData = filename
    End Sub
End Class